package com.hanxing.kxcharge.module.operations.service.costtemplate;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import com.hanxing.kxcharge.module.operations.controller.admin.costtemplate.vo.*;
import com.hanxing.kxcharge.module.operations.dal.dataobject.costtemplate.CostTemplateDO;
import com.hanxing.kxcharge.framework.common.pojo.PageResult;
import com.hanxing.kxcharge.framework.common.pojo.PageParam;
import com.hanxing.kxcharge.framework.common.util.object.BeanUtils;

import com.hanxing.kxcharge.module.operations.dal.mysql.costtemplate.CostTemplateMapper;

import static com.hanxing.kxcharge.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.hanxing.kxcharge.module.operations.enums.ErrorCodeConstants.*;

/**
 * 计费模板 Service 实现类
 *
 * @author 航兴充电
 */
@Service
@Validated
public class CostTemplateServiceImpl implements CostTemplateService {

    @Resource
    private CostTemplateMapper costTemplateMapper;

    @Override
    public Long createCostTemplate(CostTemplateSaveReqVO createReqVO) {
        // 插入
        CostTemplateDO costTemplate = BeanUtils.toBean(createReqVO, CostTemplateDO.class);
        costTemplateMapper.insert(costTemplate);
        // 返回
        return costTemplate.getId();
    }

    @Override
    public void updateCostTemplate(CostTemplateSaveReqVO updateReqVO) {
        // 校验存在
        validateCostTemplateExists(updateReqVO.getId());
        // 更新
        CostTemplateDO updateObj = BeanUtils.toBean(updateReqVO, CostTemplateDO.class);
        costTemplateMapper.updateById(updateObj);
    }

    @Override
    public void deleteCostTemplate(Long id) {
        // 校验存在
        validateCostTemplateExists(id);
        // 删除
        costTemplateMapper.deleteById(id);
    }

    private void validateCostTemplateExists(Long id) {
        if (costTemplateMapper.selectById(id) == null) {
            throw exception(COST_TEMPLATE_NOT_EXISTS);
        }
    }

    @Override
    public CostTemplateDO getCostTemplate(Long id) {
        return costTemplateMapper.selectById(id);
    }

    @Override
    public PageResult<CostTemplateDO> getCostTemplatePage(CostTemplatePageReqVO pageReqVO) {
        return costTemplateMapper.selectPage(pageReqVO);
    }

}